package edu.lingnan.lingnanblog.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//已经过时，WebMvcConfigurer接口中有它的全部方法（WebMvcConfigurerAdapter适配器，相当于中间商）
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
// * @author wuyuan
// * @since 2020-06-04 10:01
// */
////使用WebMvcConfigurer来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

/*    public void addViewControllers(ViewControllerRegistry registry) {
        // super.addViewControllers(registry);
        //浏览器发送 /test 请求来到 success
        registry.addViewController("/test").setViewName("test");
        registry.addViewController("/test.html").setViewName("test");
    }*/

    //多个请求同时生效（所以组件一起起作用）
    @Bean//将组件注册在容器中
    public WebMvcConfigurer webMvcConfigurer(){
        WebMvcConfigurer adapter = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/index.html").setViewName("index");
            }

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源；  *.css , *.js
                //SpringBoot已经做好了静态资源映射
                //拦截所以/admin/路径下的请求，除了excludePathPatterns下面开放的请求：
                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
                        .excludePathPatterns("/consumer","/consumer/blogger/toLogin","/consumer/blogger/login");
            }
        };
        return adapter;
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//
//    }
}
